/*
 * File:   main.c
 * Author: Evan
 *
 * Created on May 30, 2011, 1:25 PM
 */

#include <16F887.h>
#include <debounce.h>
#include <patterns.h>
#fuses NOPROTECT,NOWDT,INTRC,NOLVP,BROWNOUT
#use delay (clock=8M)

#define LED0    PIN_D0
#define LED1    PIN_D1
#define LED2    PIN_D2

int counter1;   //PWM sawtooth signal
int LED0duty;       //PWM duty cycle setting for all LEDs
int LED1duty;
int LED2duty;
int btncount;

#int_timer0     //On timer 0, update current duty cycles for all LEDs
increment()
{
    btncount = debounce(PIN_B0,btncount); //Function to debounce and increment btncount

    if (btncount > 3) btncount = 0; //reset button after 3
    else if (btncount == 1)
    {
        LED0duty = LED0blink();
        LED1duty = LED1blink();
        LED2duty = LED2blink();
    }
    else if (btncount == 2)
    {
        LED0duty = LED0dblblink();
        LED1duty = LED1dblblink();
        LED2duty = LED2dblblink();
    }
    else if (btncount == 3)
    {
        LED0duty = LED0pattern1();
        LED1duty = LED1pattern1();
        LED2duty = LED2pattern1();
    }
    else    //reset LEDs when not in use
    {
        LED0duty = 0;
        LED1duty = 0;
        LED2duty = 0;
    }
}

void main()
{
    setup_timer_0(T0_INTERNAL|T0_8_BIT|T0_DIV_256);
    enable_interrupts(INT_TIMER0);
    enable_interrupts(GLOBAL);

    counter1 = 0;
    LED0duty = 0;
    LED1duty = 0;
    LED2duty = 0;
    btncount = 0;

    while(1){
        counter1++;             //increment counter to generate PWM signal

        if (counter1 < LED0duty) output_high(LED0);
        else output_low(LED0);
        if (counter1 < LED1duty) output_high(LED1);
        else output_low(LED1);
        if (counter1 < LED2duty) output_high(LED2);
        else output_low(LED2);
    }
}